<?php
/**
 * User模型
 * © iDoo
 * @ admin@idooweb.com
 */
class User_Model {

    private $db;
    private $table_name='user';

    function __construct() {
        $this->db = Database::getInstance();
    }

    function getUsers($page = null) {
        $condition = '';
        if ($page) {
            $perpage_num = 15;
            $startId = ($page - 1) * $perpage_num;
            $condition = "LIMIT $startId, ".$perpage_num;
        }
        $res = $this->db->query("SELECT * FROM ".DB_PREFIX.$this->table_name." $condition");
        $users = array();
        while($row = $this->db->fetch_array($res)) {
            $row['nickname'] = htmlspecialchars($row['nickname']);
            $row['username'] = htmlspecialchars($row['username']);
            $row['email'] = htmlspecialchars($row['email']);
            $row['description'] = htmlspecialchars($row['description']);
            $users[] = $row;
        }
        return $users;
    }

    function getOneUser($uid) {
        $row = $this->db->once_fetch_array("select * from ".DB_PREFIX.$this->table_name." where uid=$uid");
        $userData = array();
        if($row) {
            $userData = array(
                'uid' => intval($row['uid']),
                'username' => htmlspecialchars($row['username']),
                'nickname' => htmlspecialchars($row['nickname']),
                'email' => htmlspecialchars($row['email']),
                'phone' => htmlspecialchars($row['phone']),
                'description' => htmlspecialchars($row['description']),
                'role' => $row['role'],
            );
        }
        return $userData;
    }

    function updateUser($userData, $uid) {
        if($userData['uid']){
            unset($userData['uid']);
        }
        $Item = array();
        foreach ($userData as $key => $data) {
            $Item[] = "$key='$data'";
        }
        $upStr = implode(',', $Item);
        $this->db->query("update ".DB_PREFIX.$this->table_name." set $upStr where uid=$uid");
    }

    function addUser($userData) {
        if($userData['uid']){
            unset($userData['uid']);
        }
        $kItem = array();
        $dItem = array();
        foreach ($userData as $key => $data) {
            $kItem[] = $key;
            $dItem[] = $data;
        }
        $field = implode(',', $kItem);
        $values = "'" . implode("','", $dItem) . "'";
        $this->db->query("INSERT INTO " . DB_PREFIX . $this->table_name." ($field) VALUES ($values)");
        $id = $this->db->insert_id();
        return $id;
    }

    function deleteUser($uid) {
        //$this->db->query("update ".DB_PREFIX."blog set author=1 and checked='y' where author=$uid");
        if($uid==1){
            exit('{"result":101,"msg":"不能删除网站创始人！"}');
        }
        $this->db->query("delete from ".DB_PREFIX.$this->table_name." where uid=$uid");
    }

    /**
     * 判断用户名是否存在
     *
     * @param string $login
     * @param int $uid 兼容更新作者资料时用户名未变更情况
     * @return boolean
     */
    function isUserExist($login, $uid = '') {
        $subSql = $uid ? 'and uid!='.$uid : '';
        $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM ".DB_PREFIX.$this->table_name." WHERE username='$login' $subSql");
        if ($data['total'] > 0) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 判断用户昵称是否存在
     *
     * @param string $nickname
     * @param int $uid 兼容更新作者资料时用户名未变更情况
     * @return boolean
     */
    function isNicknameExist($nickname, $uid = '') {
        if(empty($nickname)) {
            return FALSE;
        }
        $subSql = $uid ? 'and uid!='.$uid : '';
        $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM ".DB_PREFIX.$this->table_name." WHERE nickname='$nickname' $subSql");
        if ($data['total'] > 0) {
            return true;
        }else {
            return false;
        }
    }

    function getUserNum() {
        $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM ".DB_PREFIX.$this->table_name);
        return $data['total'];
    }
}
